package cn.wetcland.urban.framework.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.framework.dto.AddressAddDTO;
import cn.wetcland.urban.framework.dto.AddressUpdateDTO;
import cn.wetcland.urban.framework.mapper.UtCustomerAddressMapper;
import cn.wetcland.urban.framework.mapper.UtCustomerMapper;
import cn.wetcland.urban.framework.mapper.UtOpenCityMapper;
import cn.wetcland.urban.framework.service.UtCustomerAddressService;
import cn.wetcland.urban.framework.service.UtDistrictService;
import cn.wetcland.urban.framework.service.UtOpenCityService;
import cn.wetcland.urban.framework.vo.CustomerAddressListItemVO;
import cn.wetcland.urban.generator.domain.UtCustomerAddress;
import cn.wetcland.urban.generator.domain.UtOpenCity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author hongyao.tan
* @description 针对表【ut_customer_address】的数据库操作Service实现
* @createDate 2024-03-07 17:17:41
*/
@Service
public class UtCustomerAddressServiceImpl extends ServiceImpl<UtCustomerAddressMapper, UtCustomerAddress> implements UtCustomerAddressService {

    @Resource
    public UtCustomerAddressMapper customerAddressMapper;

    @Resource
    public UtDistrictService districtService;

    @Resource
    public UtOpenCityService openCityService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAddress(AddressAddDTO addressAddDTO) {
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        boolean b = openCityService.verifyOpenCityExist(addressAddDTO.getAddressCityId());
        MyAssert.isTrue(b, "该城市未开放");
        boolean verifiedDistrict = districtService.verifyDistrict(addressAddDTO.getAddressProvinceId(), addressAddDTO.getAddressCityId(), addressAddDTO.getAddressDistrictId());
        MyAssert.isTrue(verifiedDistrict, "省市区不匹配");
        UtCustomerAddress utCustomerAddress = new UtCustomerAddress();
        utCustomerAddress.setCustomerId(loginIdAsLong);
        CopyObjectUtil.copyPropertiesIgnoreNull(addressAddDTO, utCustomerAddress);
        customerAddressMapper.insert(utCustomerAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAddress(Long id) {
        customerAddressMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAddress(AddressUpdateDTO addressUpdateDTO) {
        UtCustomerAddress utCustomerAddress = customerAddressMapper.selectById(addressUpdateDTO.getAddressId());
        MyAssert.notNull(utCustomerAddress, "该地址不存在");
        boolean verifiedDistrict = districtService.verifyDistrict(addressUpdateDTO.getAddressProvinceId(), addressUpdateDTO.getAddressCityId(), addressUpdateDTO.getAddressDistrictId());
        MyAssert.isTrue(verifiedDistrict, "省市区不匹配");
        CopyObjectUtil.copyPropertiesIgnoreNull(addressUpdateDTO, utCustomerAddress);
        customerAddressMapper.updateById(utCustomerAddress);
    }

    @Override
    public List<CustomerAddressListItemVO> getAddress() {
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        return customerAddressMapper.selectCustomerAddressList(loginIdAsLong);
    }
}




